package com.eckey.lab.service.impl;

import com.alibaba.fastjson.JSON;
import com.eckey.lab.bean.User;
import com.eckey.lab.common.ResultData;
import com.eckey.lab.enums.ExceptionEnum;
import com.eckey.lab.exception.BaseException;
import com.eckey.lab.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author: Marinc
 * @CreateTime: 2024-06-17  14:38
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Override
    public ResultData addUser(User user) {
        if (StringUtils.isEmpty(user.getUserName())) {
            log.error("userName不能为空！");
            throw new BaseException(ExceptionEnum.CODE_8005, "userName");
        }
        if (user.getAge() <= 0) {
            log.error("age不能为负数");
            throw new BaseException(ExceptionEnum.CODE_8005, "age");
        }
        //处理入库
        log.info("数据入库成功：{}", JSON.toJSONString(user));
        return ResultData.success();
    }

    @Override
    public ResultData queryById(Long id) {
        if (id == null || id <= 0) {
            log.error("id不能为空！");
            throw new BaseException(ExceptionEnum.CODE_8005, "id不能为空");
        }
        final User user = new User();
        user.setAddress("浙江省杭州市拱墅区朝晖路3号");
        user.setAge(18);
        user.setGender("男");
        user.setUserName("马张飞");
        return ResultData.success(JSON.toJSONString(user));
    }

}
